package com.learn.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learn.order.domain.Order;
import com.learn.order.mapper.OrderMapper;
import com.learn.order.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {


    @Resource
    private OrderMapper orderMapper;

    @Override
    public void createOrder(Order order) {
        // 保存订单至数据库
        saveOrderToDataBase(order);
        // 将订单Id推送至延迟队列

    }

    @Override
    public void cancelOrder(String orderId) {
        // 取消订单
    }

    @Override
    public List<Order> getUnpaidOrders() {
        return null;
    }


    /**
     * Redis的键过期事件
     * 当键过期时，Redis会自动调用该方法（需要配置Redis的过期事件通知功能）
     * @param orderId
     */
    public void onOrderKeyExpired(String orderId) {
        cancelOrder(orderId);
    }
    private void saveOrderToDataBase(Order order) {
        // 订单入库
        orderMapper.insert(order);
    }
}
